﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DeviceInfoManage.BusinessLogicLayer;

public partial class DutyManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["adminUserName"] == null)
            {
                Response.Redirect("logout.aspx");
            }
            if (Session["adminType"].ToString() != "管理员")
            {
                Btn_Add.Enabled = false;
            }
            if (!string.IsNullOrEmpty(Request.QueryString["type"]))
            {
                hidType.Value = Request.QueryString["type"];
            }
            else
            {
                Response.Write("<script>alert('参数错误!');</script>");
            }
            InitData();
            if (hidType.Value == "2")
            {
                BindData();
                Btn_Add.Text = "更新";
            }
            else
            {
                ddlPrincipal.SelectedIndex = 0;
                txtPhone.Text = (new MaintPerson()).GetPhoneById(int.Parse(ddlPrincipal.SelectedValue));
                trResponse.Visible = false;
            }
        }
    }

    private void InitData()
    {
        ddlPrincipal.DataSource = (new MaintPerson()).GetMaintPersonName();
        ddlPrincipal.DataTextField = "maintPersonName";
        ddlPrincipal.DataValueField = "maintPersonId";
        ddlPrincipal.DataBind();
    }

    private void BindData()
    {
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            hidId.Value = Request.QueryString["id"];
        }
        DutyArrange dag = (new DutyArrange()).GetModelFromView(int.Parse(hidId.Value));
        ddlPrincipal.SelectedValue = dag.Person;
        txtDate.Text = dag.Date.ToString("yyyy-MM-dd");
        txtDescription.Text = dag.Description;
        txtResponse.Text = dag.Response;
        txtPhone.Text = dag.PhoneNumber;
        if (dag.Person != Session["adminUserName"].ToString())
        {
            btnResponse.Enabled = false;
        }
        if (dag.Response != "")
        {
            Btn_Add.Enabled = false;
            if (Session["adminType"].ToString() == "管理员")
            {
                btnDel.Visible = true;
            }
        }
    }

    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        DutyArrange dag = new DutyArrange();
        dag.Person = ddlPrincipal.SelectedValue;
        dag.Date = Convert.ToDateTime(txtDate.Text);
        dag.Description = txtDescription.Text;
        dag.Response = txtResponse.Text;

        if (hidId.Value == "")
        {
            int i = dag.Add(dag);
            if (i != -1)
                Response.Write("<script>alert('添加成功!');location.href=location.href;</script>");
            else
                Response.Write("<script>alert('添加失败!');</script>");
        }
        else
        {
            dag.ID = int.Parse(hidId.Value);
            dag.Update(dag);
            Response.Write("<script>alert('更新成功!');location.href=location.href;</script>");
        }

    }

    protected void ddlPrincipal_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtPhone.Text = (new MaintPerson()).GetPhoneById(int.Parse(ddlPrincipal.SelectedValue));
    }

    protected void btnResponse_Click(object sender, EventArgs e)
    {
        string strSql = string.Format("UPDATE [DutyArrange] SET Response = '{0}' WHERE ID = '{1}'", txtResponse.Text, hidId.Value);
        DeviceInfoManage.DataAccessLayer.DbHelperSQL.GetSingle(strSql.ToString());
        Response.Write("<script>alert('反馈成功!');location.href=location.href;</script>");
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        string strSql = string.Format("UPDATE [DutyArrange] SET Response = '' WHERE ID = '{0}'", hidId.Value);
        DeviceInfoManage.DataAccessLayer.DbHelperSQL.GetSingle(strSql.ToString());
        Response.Write("<script>alert('操作成功!');location.href=location.href;</script>");
    }
}
